from django.test import RequestFactory, TestCase, Client
from juqiadmin.models import JuqiAdmin, get_game_finance
import json
from rest_framework import status
from .models import (
    QuanjushangGame,
    QuanjushangSonGame,
    QuanjushangGood,
)
from wxuser.models import Users
from warehouse.models import JuqiGood, GoodLevel


class QuanjushangMethodX(TestCase):
    """
    全局赏模式类型设置
    """

    def setUp(self):
        # Every test needs access to the request factory.
        self.c = Client()
        self.factory = RequestFactory()
        self.admin_user = {"user": "admin", "password": "admin123"}
        u = JuqiAdmin(**self.admin_user)
        u.save()

    @classmethod
    def create_quanju(cls):
        # 创建商品类型 创建池子 池子内加入商品
        A_good = GoodLevel.objects.create(
            title="A商品", desc="卡卡好的小汽车", game_type=1, img_url="123"
        )

        B_good = GoodLevel.objects.create(
            title="B商品", desc="卡卡好的小火车", game_type=1, img_url="123"
        )

        quanjushang_good = GoodLevel.objects.create(
            title="全局赏", desc="全局赏给的飞机", game_type=1, method_type=1, img_url="123"
        )

        kongcheshang_good = GoodLevel.objects.create(
            title="空车赏*4",
            desc="空车赏 * 4 奖励",
            game_type=1,
            num=4,
            method_type=2,
            img_url="123",
        )

        chuanchuan_good = GoodLevel.objects.create(
            title="一串2奖励",
            desc="一串2获得直升机",
            game_type=1,
            num=2,
            method_type=3,
            img_url="123",
        )

        return A_good, B_good, quanjushang_good, kongcheshang_good, chuanchuan_good

    # 创建游戏商品类型
    def test_game_method(self):
        # login
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)

        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        r = self.c.post(
            "/api/warehouse/goodlevel",
            {
                "title": "一串6",
                "desc": "获得6件商品可以获得额外商品",
                "game_type": 1,
                "method_type": 3,
                "num": 6,
                "img_url": "123",
            },
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_201_CREATED)

        d = json.loads(r.content)
        print(d)
        self.assertEqual(d["code"], 0)

    # 获取全部游戏商品类型
    def test_getgame_method(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)

        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        r = self.c.get(
            "/api/quanjushang/game",
            {"page": 1, "limit": 10},
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        res = json.loads(r.content)

    # 创建池子
    def test_create_game(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)

        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        r = self.c.post(
            "/api/quanjushang/game",
            {
                "name": "小汽车滴滴123",
                "cover_url": "http://ss",
                "user_limit": 10,
                "price": 10,
                "game_num": 500,
            },
            content_type="application/json",
        )
        res = json.loads(r.content)
        self.assertEqual(res["code"], 0)

    # 在池子中加入商品 修改赏品 删除赏品
    def test_put_good(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)

        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        m = GoodLevel.objects.create(
            title="A商品", desc="普通商品", game_type=1, num=0, img_url="123"
        )

        v = QuanjushangGame.objects.create(
            name="小汽车抽奖", cover_url="封面Url", user_limit=1, price=50, game_num=500
        )

        r = self.c.post(
            "/api/quanjushang/good",
            {
                "name": "小汽车666",
                "ip_famliy": "",
                "display_url": "123",
                "desc_url": "456",
                "price": 50,
                "recovery_price": 30,
                "desc": "普通商品",
                "game_uuid": v.uuid,
                "method_uuid": m.uuid,
                "count": 5,
            },
            content_type="application/json",
        )
        res = json.loads(r.content)
        self.assertEqual(res["code"], 0)
        good_uuid = res["data"]["uuid"]
        r = self.c.put(
            "/api/quanjushang/gamegoods",
            {
                "name": "改吧",
                "ip_famliy": "",
                "display_url": "httos/:123",
                "desc_url": "456",
                "price": 300,
                "recovery_price": 10,
                "desc": "哈哈哈哈哈哈赏品",
                "game_uuid": v.uuid,
                "method_uuid": m.uuid,
                "good_uuid": good_uuid,
            },
            content_type="application/json",
        )
        res = json.loads(r.content)
        self.assertEqual(res["code"], 0)

        r = self.c.delete(
            "/api/quanjushang/gamegoods",
            {"game_uuid": v.uuid, "good_uuid": good_uuid},
            content_type="application/json",
        )
        res = json.loads(r.content)
        self.assertEqual(res["code"], 0)

    # 获取池子下的赏品信息
    def test_get_gamegoods(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)

        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        chizi = QuanjushangGame.objects.create(
            name="test池子", cover_url="123", user_limit=1, price=10, game_num=50
        )

        songame_ = []
        for v in range(1, 50 + 1):
            i = {}
            i["game_uuid"] = chizi.uuid
            i["user_limit"] = 1
            i["price"] = 10
            i["box_num"] = v
            songame_.append(QuanjushangSonGame(**i))

        QuanjushangSonGame.objects.bulk_create(songame_)

        r = self.c.get(
            "/api/quanjushang/gamegoods",
            {"game_uuid": chizi.uuid},
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

    # 修改池子状态
    def test_put_game(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)

        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        chizi = QuanjushangGame.objects.create(
            name="test池子", cover_url="123", user_limit=1, price=10, game_num=50
        )
        songame_ = []
        for v in range(1, 50 + 1):
            i = {}
            i["game_uuid"] = chizi.uuid
            i["user_limit"] = 1
            i["price"] = 10
            i["box_num"] = v
            songame_.append(QuanjushangSonGame(**i))

        QuanjushangSonGame.objects.bulk_create(songame_)

        r = self.c.put(
            "/api/quanjushang/game",
            {"game_uuid": chizi.uuid, "radio": 1},
            content_type="application/json",
        )
        d = json.loads(r.content)

        self.assertEqual(r.status_code, status.HTTP_200_OK)
        self.assertEqual(d["code"], 0)

    # 修改池子全局参数
    def test_update_game(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)

        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        chizi = QuanjushangGame.objects.create(
            name="test池子", cover_url="123", user_limit=1, price=10, game_num=50
        )

        songame_ = []
        for v in range(1, 50 + 1):
            i = {}
            i["game_uuid"] = chizi.uuid
            i["user_limit"] = 1
            i["price"] = 10
            i["box_num"] = v
            songame_.append(QuanjushangSonGame(**i))

        QuanjushangSonGame.objects.bulk_create(songame_)

        r = self.c.put(
            "/api/quanjushang/gameupdate",
            {
                "name": "马云昊测试888",
                "cover_url": "http://sscc",
                "user_limit": 15,
                "price": 500,
                "game_uuid": chizi.uuid,
            },
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)


# 小程序端展示
class QuanjushangXcx(TestCase):
    """ """

    def setUp(self):
        # Every test needs access to the request factory.
        self.c = Client(HTTP_X_WX_OPENID="wx123")
        self.factory = RequestFactory()

    @classmethod
    def create_a_user(cls):
        d = {
            "nickname": "xiaoming",
            "head_url": "http://nick",
            "openid": "wx123",
            "stored_money": 1000000,
            "score": 0,
        }
        u = Users(**d)
        u.save()
        return u

    def test_get_chizi(self):
        r = self.c.get(
            "/api/quanjushang/raffle",
            {"page": 1, "limit": 10},
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        res = json.loads(r.content)

    def test_get_gameson(self):
        # 创建池子和对局

        chizi = QuanjushangGame.objects.create(
            name="test池子", cover_url="123", user_limit=1, price=10, game_num=50
        )

        songame_ = []
        for v in range(1, 50 + 1):
            i = {}
            i["game_uuid"] = chizi.uuid
            i["user_limit"] = 1
            i["price"] = 10
            i["box_num"] = v
            songame_.append(QuanjushangSonGame(**i))

        QuanjushangSonGame.objects.bulk_create(songame_)

        # 获取游戏下得箱子
        r = self.c.get(
            "/api/quanjushang/songame",
            {"page": 1, "limit": 10, "game_uuid": chizi.uuid},
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        res = json.loads(r.content)

        a = QuanjushangSonGame.objects.filter(game_uuid=chizi.uuid).first()

        A_good = GoodLevel.objects.create(
            title="A商品", desc="卡卡好的小汽车", game_type=1, img_url="123"
        )

        jq_good = JuqiGood.objects.create(
            name="小汽车",
            display_url="123",
            price=50,
            recovery_price=10,
            game_type="全局赏",
            ip_family="123",
            # 商品详细介绍
            detail="123",
        )
        # 获取游戏下的箱子
        game_uuid = chizi.uuid

        # 查看什么商品类型
        method_uuid = A_good.uuid

        # 模式加入商品库
        method_ = GoodLevel.objects.filter(uuid=method_uuid).first()

        # 创建商品数量
        count = 10
        ##0普通商品 1全局赏商品 2空车赏 3串串赏
        method_level = method_.game_type

        # 创建商品list
        goods_ = []
        if method_level == 2 or method_level == 3:
            # 获取到所有箱子信息 在箱子中添加各个箱子的商品
            son_game = QuanjushangSonGame.objects.filter(game_uuid=game_uuid).all()
            for v in son_game:
                i = {}
                i["son_uuid"] = v.uuid
                i["good_uuid"] = jq_good.uuid
                i["count"] = count
                i["price"] = 50
                i["rf_count"] = count
                i["good_name"] = "小汽车"
                i["good_type"] = method_level
                i["recovery_price"] = 10
                i["good_desc"] = "123"
                i["good_url"] = "123"
                i["good_descurl"] = "123"
                i["num"] = method_.num
                goods_.append(QuanjushangGood(**i))
        else:
            # 获取到所有箱子信息 在箱子中添加各个箱子的商品
            son_game = QuanjushangSonGame.objects.filter(game_uuid=game_uuid).all()
            for v in son_game:
                i = {}
                i["son_uuid"] = v.uuid
                i["good_uuid"] = jq_good.uuid
                i["count"] = count
                i["price"] = 50
                i["rf_count"] = count
                i["good_name"] = "小汽车"
                i["good_type"] = method_level
                i["recovery_price"] = 10
                i["good_desc"] = "123"
                i["good_url"] = "123"
                i["good_descurl"] = "123"
                goods_.append(QuanjushangGood(**i))

        QuanjushangGood.objects.bulk_create(goods_)

        # 查看箱子下的信息
        r = self.c.get(
            "/api/quanjushang/gamegood",
            {"sonuuid": a.uuid},
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        res = json.loads(r.content)

    def test_post_raffle(self):
        QuanjushangXcx.create_a_user()

        chizi = QuanjushangGame.objects.create(
            name="test池子", cover_url="123", user_limit=1, price=10, game_num=50, state=1
        )

        songame_ = []
        for v in range(1, 50 + 1):
            i = {}
            i["game_uuid"] = chizi.uuid
            i["user_limit"] = 1
            i["price"] = 10
            i["box_num"] = v
            songame_.append(QuanjushangSonGame(**i))

        QuanjushangSonGame.objects.bulk_create(songame_)

        # 获取游戏下得箱子
        r = self.c.get(
            "/api/quanjushang/songame",
            {"page": 1, "limit": 10, "game_uuid": chizi.uuid},
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        res = json.loads(r.content)

        a = QuanjushangSonGame.objects.filter(game_uuid=chizi.uuid).first()

        A_good = GoodLevel.objects.create(
            title="A商品", desc="卡卡好的小汽车", game_type=1, img_url="123"
        )

        jq_good = JuqiGood.objects.create(
            name="小汽车",
            display_url="123",
            price=50,
            recovery_price=10,
            game_type="全局赏",
            ip_family="123",
            # 商品详细介绍
            detail="123",
        )
        # 获取游戏下的箱子
        game_uuid = chizi.uuid

        # 查看什么商品类型
        method_uuid = A_good.uuid

        # 模式加入商品库
        method_ = GoodLevel.objects.filter(uuid=method_uuid).first()

        # 创建商品数量
        count = 10
        ##0普通商品 1全局赏商品 2空车赏 3串串赏
        method_level = method_.game_type

        # 创建商品list
        goods_ = []
        if method_level == 2 or method_level == 3:
            # 获取到所有箱子信息 在箱子中添加各个箱子的商品
            son_game = QuanjushangSonGame.objects.filter(game_uuid=game_uuid).all()
            for v in son_game:
                i = {}
                i["son_uuid"] = v.uuid
                i["good_uuid"] = jq_good.uuid
                i["count"] = count
                i["price"] = 50
                i["rf_count"] = count
                i["good_name"] = "小汽车"
                i["good_type"] = method_level
                i["recovery_price"] = 10
                i["good_desc"] = "123"
                i["good_url"] = "123"
                i["good_descurl"] = "123"
                i["num"] = method_.num
                goods_.append(QuanjushangGood(**i))
        else:
            # 获取到所有箱子信息 在箱子中添加各个箱子的商品
            son_game = QuanjushangSonGame.objects.filter(game_uuid=game_uuid).all()
            for v in son_game:
                i = {}
                i["son_uuid"] = v.uuid
                i["good_uuid"] = jq_good.uuid
                i["count"] = count
                i["price"] = 50
                i["rf_count"] = count
                i["good_name"] = "小汽车"
                i["good_type"] = method_level
                i["recovery_price"] = 10
                i["good_desc"] = "123"
                i["good_url"] = "123"
                i["good_descurl"] = "123"
                goods_.append(QuanjushangGood(**i))

        QuanjushangGood.objects.bulk_create(goods_)

        r = self.c.post(
            "/api/quanjushang/raffle",
            {
                "son_uuid": a.uuid,
                "pay_type": "stored_money",
                "raffle_count": 1,
            },
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)
        r = json.loads(r.content)
